#include "TesPch.h"
#include "TesBlockParser.h"
#include "TesParserError.h"
#include "TesAssignStatementParser.h"


TesBlockParser::TesBlockParser( const TesConfig & config, TesTokenStream & tokens )
: TesParserBase( config, tokens )
{
}


bool TesBlockParser::Parse()
{
	const TesTokenStream & tokens = this->GetTokens();

	list<const TesStatementCode *> statements;

	while( tokens.IsEndOfStream() == false )
	{
		const TesToken & curToken = tokens.GetCurrent();
		switch( curToken.Type )
		{
		case TTT_Identifier:
			{
				int openParentheseIndex = tokens.SearchForward( TTT_OpenParenthese );
				int assignIndex = tokens.SearchForward( TTT_OpAssign );
				if( openParentheseIndex == -1 && assignIndex == -1 )
				{
					TesParserError( this->GetConfig(), t("Assign statement or function call statement is required."), curToken.Location );
					return false;
				}
				if( openParentheseIndex == -1 || ( openParentheseIndex != -1 && assignIndex < openParentheseIndex ) )
				{
					TesAssignStatementParser assignStatementParser( this->GetConfig(), this->GetTokens() );
					if( assignStatementParser.Parse() == false )
					{
						return false;
					}
					statements.push_back( assignStatementParser.GetAssignment() );
					continue;
				}
				if( assignIndex == -1 || ( assignIndex != -1 && openParentheseIndex < assignIndex ) )
				{
					// TODO: Add FunctionCallParser.Parse() here.
					continue;
				}
			}
			break;
		default:
			break;
		}
	}
	return true;
}